<template>
  <el-dialog
    v-model="visible"
    title="添加滑坡记录"
    width="40%"
    :close-on-click-modal="false"
  >
    <el-form :model="form" label-width="100px">
      <el-form-item label="地点">
        <el-input v-model="form.local" placeholder="请输入监测点位置"></el-input>
      </el-form-item>

      <el-form-item label="坐标">
        <el-input v-model="form.location" placeholder="请输入坐标，如 (2371787,772861)"></el-input>
      </el-form-item>

      <el-form-item label="时间">
        <el-date-picker
          v-model="form.time"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%;"
        />
      </el-form-item>

      <el-form-item label="规模">
        <el-select v-model="form.scale" placeholder="请选择规模">
          <el-option label="小型" value="小型" />
          <el-option label="中等" value="中等" />
          <el-option label="较大" value="较大" />
          <el-option label="特大" value="特大" />
        </el-select>
      </el-form-item>

      <el-form-item label="成因">
        <el-input v-model="form.cause" placeholder="请输入成因"></el-input>
      </el-form-item>

      <el-form-item label="报告">
        <el-button type="primary" @click="addReport">添加报告</el-button>
        <span v-if="reportAdded" style="margin-left: 10px; color: green;">已添加</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="saveRecord">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";

const visible = ref(false);

const form = ref({
  id: null,
  local: "",
  location: "",
  time: "",
  scale: "",
  cause: ""
});

const reportAdded = ref(false);

// 打开弹窗
const open = () => {
  visible.value = true;
  resetForm();
};

// 模拟添加报告
const addReport = () => {
  reportAdded.value = true;
  console.log("报告功能未实现，仅做演示");
  ElMessage.success("报告已添加（模拟）");
};

// 模拟保存
const saveRecord = () => {
  console.log("保存滑坡记录:", form.value);
  ElMessage.success("滑坡记录已保存（模拟）");
  visible.value = false;
};

const resetForm = () => {
  form.value = {
    id: null,
    local: "",
    location: "",
    time: "",
    scale: "",
    cause: ""
  };
  reportAdded.value = false;
};

defineExpose({ open });
</script>
